/*
 * Isman Usoh<isman.usoh@gmail.com>
 * www.facebook.com/isman.usoh
 */
package com.isman.postcodeinfo;

import java.util.ArrayList;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.isman.postcodeinfo.database.PostCode;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class DetailActivity extends MapActivity {

	protected static final String DATA = "data";
	protected static final String INDEX = "index";

	private ArrayList<PostCode> mPostCodes = null;
	private int mIndex = 0;
	private MyItemizedOverlay postOfficeOverlay;

	private TextView tvName;
	private TextView tvTitle;
	private TextView tvSubTitle;
	private MapView mapView;
	private ImageButton ibtnFF;
	private ImageButton ibtnRew;
	private Button btnView;
	private MapController mapControll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		getActionBar().setDisplayShowHomeEnabled(true);
		getActionBar().setDisplayShowTitleEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		tvName = (TextView) findViewById(R.id.tvdetailname);
		tvTitle = (TextView) findViewById(R.id.tvdetailtitle);
		tvSubTitle = (TextView) findViewById(R.id.tvdetailsubtitle);
		mapView = (MapView) findViewById(R.id.mapview);
		ibtnFF = (ImageButton) findViewById(R.id.ibtnFF);
		ibtnRew = (ImageButton) findViewById(R.id.ibtnRew);
		btnView = (Button) findViewById(R.id.btnDetail);
		
		
		
		mPostCodes = (ArrayList<PostCode>) this.getIntent().getSerializableExtra(DATA);
		mIndex = (Integer) this.getIntent().getIntExtra(INDEX, 0);

		Log.i("Isman", "Size : " + mPostCodes + " Index : " + mIndex);

		this.getPostOfficeLocation();
		this.setLabel();

		if (mIndex == (postOfficeOverlay.getCount() - 1)) {
			ibtnFF.setEnabled(false);
		}
		if (mIndex == 0) {
			ibtnRew.setEnabled(false);
		}

		btnView.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Intent intent = new Intent(view.getContext(),
						PostMapActivity.class);
				intent.putExtra(DATA, mPostCodes.get(mIndex));
				view.getContext().startActivity(intent);
			}
		});
		ibtnFF.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i("Isman", "FF");
				mIndex++;

				if (mIndex == (postOfficeOverlay.getCount() - 1)) {
					ibtnFF.setEnabled(false);
				} else {
					ibtnFF.setEnabled(true);
					ibtnRew.setEnabled(true);
				}
				DetailActivity.this.setLabel();
			}
		});
		ibtnRew.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Log.i("Isman", "Rew");
				mIndex--;

				if (mIndex == 0) {
					ibtnRew.setEnabled(false);
				} else {
					ibtnFF.setEnabled(true);
					ibtnRew.setEnabled(true);
				}
				DetailActivity.this.setLabel();
			}
		});
	}

	private void setLabel() {
		tvName.setText(mPostCodes.get(mIndex).getName() + " ("
				+ mPostCodes.get(mIndex).getPostcodel() + ")");
		tvTitle.setText(mPostCodes.get(mIndex).getAddress()
				+ mPostCodes.get(mIndex).getDistrict());
		tvSubTitle.setText(mPostCodes.get(mIndex).getProvince());

		postOfficeOverlay.move(mIndex);
	}

	private void getPostOfficeLocation() {
		mapControll = mapView.getController();
		mapView.setBuiltInZoomControls(false);
		mapView.setClickable(false);
		mapControll.setZoom(16);

		postOfficeOverlay = new MyItemizedOverlay(this.getResources().getDrawable(R.drawable.postoffice));
		for (int i = 0; i < mPostCodes.size(); i++) {
			GeoPoint currentPoint = new GeoPoint((int) (mPostCodes.get(i).getLlat() * 1E6),(int) (mPostCodes.get(i).getLlong() * 1E6));
			OverlayItem item = new OverlayItem(currentPoint, "ไปรษณี "+ mPostCodes.get(i).getName(), null);
			postOfficeOverlay.addOverlay(item);

			if (mIndex == i) {
				mapControll.animateTo(currentPoint);
			}
		}
		mapView.getOverlays().add(postOfficeOverlay);
		mapView.postInvalidate();
	}

	@Override
	protected void onResume() {
		mapView.setBuiltInZoomControls(false);
		mapView.setClickable(false);
		mapControll.setZoom(16);
		
		postOfficeOverlay.move(mIndex);
		
		super.onResume();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.e("Isman", item.getItemId() + " " + android.R.id.home);
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent i = new Intent(DetailActivity.this, MainActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> myOverlays;

		public MyItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
			myOverlays = new ArrayList<OverlayItem>();
			populate();
		}

		public void addOverlay(OverlayItem overlay) {
			myOverlays.add(overlay);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return myOverlays.get(i);
		}

		public void removeItem(int i) {
			myOverlays.remove(i);
			populate();
		}

		@Override
		public int size() {
			return myOverlays.size();
		}

		public void addOverlayItem(OverlayItem overlayItem) {
			myOverlays.add(overlayItem);
			populate();
		}

		public void move(int index) {
			mapView.getController().animateTo(myOverlays.get(index).getPoint());
		}

		public void addOverlayItem(int lat, int lon, String title) {
			try {
				GeoPoint point = new GeoPoint(lat, lon);
				OverlayItem overlayItem = new OverlayItem(point, title, null);
				addOverlayItem(overlayItem);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

		public int getCount() {
			return this.myOverlays.size();
		}

		@Override
		protected boolean onTap(int index) {
			mIndex = index;
			if (mIndex == 0) {
				ibtnRew.setEnabled(false);
			}
			if (mIndex == (postOfficeOverlay.getCount() - 1)) {
				ibtnFF.setEnabled(false);
			} else {
				ibtnFF.setEnabled(true);
				ibtnRew.setEnabled(true);
			}
			DetailActivity.this.setLabel();
			return super.onTap(index);
		}
	}
}
